package bTree.LeetCode;

import java.util.*;

public class 设计推特 {

    class Node {
        int userId;
        int tweetId;
        Node next;

        public Node(int userId, int tweetId) {
            this.userId = userId;
            this.tweetId = tweetId;
        }

        public Node() {
        }
    }

    Node head;
    HashMap<Integer, Set<Integer>> hashMap;

    public Twitter() {
        head = new Node();
        hashMap = new HashMap<>();
    }

    public void postTweet(int userId, int tweetId) {
        Node node = new Node(userId, tweetId);
        node.next = head;
        head = node;
    }

    public List<Integer> getNewsFeed(int userId) {
        int cnt = 0;
        List<Integer> list = new ArrayList<>();
        Set<Integer> integers = hashMap.get(userId);
        Node p = head;
        while (p != null && cnt != 10) {
            int userId1 = p.userId;
            if (userId1 != 0) {
                if (userId1 == userId || (integers != null && integers.contains(userId1))) {
                    list.add(p.tweetId);
                    cnt++;
                }
            }
            p = p.next;
        }
        return list;
    }

    public void follow(int followerId, int followeeId) {
        if (!hashMap.containsKey(followerId)) {
            hashMap.put(followerId, new HashSet<>());
        }
        Set<Integer> integers = hashMap.get(followerId);
        integers.add(followeeId);
    }

    public void unfollow(int followerId, int followeeId) {
        Set<Integer> integers = hashMap.get(followerId);
        if (integers != null) {
            integers.remove(followeeId);

        }
    }
}

/**
 * Your Twitter object will be instantiated and called as such:
 * Twitter obj = new Twitter();
 * obj.postTweet(userId,tweetId);
 * List<Integer> param_2 = obj.getNewsFeed(userId);
 * obj.follow(followerId,followeeId);
 * obj.unfollow(followerId,followeeId);
 */